#include "LPC11xx.h"
#include "utilfun.h"
#include "network.h"
#include "gpio.h"
#include "ssp.h"

/***************************************************************
 * Macro definitions                                           *
 ***************************************************************/

/***************************************************************
 * Globol variables                                            *
 ***************************************************************/
 
static uint32_t const keyAddr[16] = {0, 2, 2, 8, 0, 3, 0, 5,
                                     3, 4, 2, 5, 0, 6, 2, 9};

/***************************************************************
 * Functions                                                   *
 ***************************************************************/
void myDelay (uint32_t ulTime)
{
    uint32_t i;

    i = 0;
    while (ulTime--) {
        for (i = 0; i < 5000; i++);
    }
}

void halDataReceived(uint8_t * data, uint32_t len)
{
	GPIOSetValue(2, 7, !GPIOGetValue(2, 7));
	utilDelayMs(200);
	GPIOSetValue(2, 7, !GPIOGetValue(2, 7));
}

void tmp122Init(void)
{
    GPIOSetDir(2, 0, GPIO_DIR_OUTPUT);  // CS
    LPC_IOCON->PIO2_0 |= (0x1 << 4);
    LPC_IOCON->PIO2_1 |= (0x1 << 4);
    LPC_IOCON->PIO2_2 |= (0x1 << 4);
    LPC_IOCON->PIO2_3 |= (0x1 << 4);

	SSP_IOConfig(1);
	SSP_Init(1);
	// set CS high.
	GPIOSetValue(2, 0, 1);
}

uint32_t readTMP122(uint8_t * sspData, uint32_t len)
{
	GPIOSetValue(2, 0, 0);
	SSP_Receive(1, sspData, len);
	GPIOSetValue(2, 0, 1);

	return len;
}

void initAddress()
{
    uint32_t i;
    for (i = 0; i < 16; i+=2) {
        GPIOSetDir(keyAddr[i], keyAddr[i+1], GPIO_DIR_INPUT);
    }
    LPC_IOCON->PIO0_5 = 0x100;  // set for generate port.
    GPIOSetValue(0, 5, 1);
}

uint8_t readAddress()
{
    uint32_t i;
    uint8_t  addr = 0;
    for (i = 0; i < 16; i+=2) {
        addr <<= 1;
        addr += GPIOGetValue(keyAddr[i], keyAddr[i+1])? 1 : 0;
    }
    
    return addr;
}

/***************************************************************
 * MAIN                                                        *
 ***************************************************************/
int main (void)
{
    uint8_t const phyaddr = 0xf2;
    uint32_t const UART_SEND_LEN = 4;
    uint8_t UARTStr[UART_SEND_LEN];

    SystemInit();             /* MUST BE INCLUDED! */

    GPIOInit();
    // LED
    GPIOSetDir(2, 7, GPIO_DIR_OUTPUT);
    GPIOSetDir(3, 3, GPIO_DIR_INPUT);

	utilInit();

    /* rfi test */
    networkInit(phyaddr, 1);
	networkSetIntCallback(halDataReceived);
    
    initAddress();
    tmp122Init();

    while (1) {

		networkProcessDuty();
        
        UARTStr[0] = readAddress();
        
        readTMP122(UARTStr+1, 2);
        
        // get light state.
        UARTStr[3] = GPIOGetValue(3, 3);

//        halrfiSend(sspData, SSP_DATA_LEN);     // send message.
        if (networkSend(UARTStr, UART_SEND_LEN)) {     // send message.
		//	GPIOSetValue(2, 7, !GPIOGetValue(2, 7));
		}
		utilDelayMs(300);
    }
}

/***************************************************************
 * END OF FILE                                                 *
 ***************************************************************/
